/**
 * 
 */
package com.hq.cms.service.impl;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;


import com.hq.cms.utils.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hq.cms.core.Page;
import com.hq.cms.dao.ArticleMapper;
import com.hq.cms.domain.Article;
import com.hq.cms.domain.Comments;
import com.hq.cms.service.ArticleService;

/**
 * 说明:
 * 
 * @author howsun ->[howsun.zhang@gmail.com]
 * @version 1.0
 *
 * 2019年4月21日 下午9:06:07
 */
@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {

	@Resource
	ArticleMapper articleMapper;

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public List<Article> gets(Article conditions, Page page, LinkedHashMap<String, Boolean> orders) {
		List<Article> articles = articleMapper.selects(conditions, orders, page);
		if(page != null && page.getPageCount() == 0){
			int totalCount = articleMapper.count(conditions);
			page.setTotalCount(totalCount);
		}
		return articles;
	}

	// 通过用户id获取文章列表
	@Override
	public List getsByUserId(Integer id) {
		
		return articleMapper.getsByUserId(id);
	}

	@Override
	public int addBlog(Article blog) {
		// 1：处理 摘要的内容 ：如果摘要不写 默认取 内容的前10个字 如果没有10个子就都取了
		if(!StringUtil.hasText(blog.getSummary())) {
			// 如果为null 或者空字符串包括空格那么 进来 处理
			String content = blog.getContent();
			String replaceAll = content.replaceAll("<.+?>", "");
			if(replaceAll.length()>10) {
				blog.setSummary(replaceAll.substring(0,10));
			}else {
				blog.setSummary(replaceAll);
			}
		}
		// 在blog对象中有 title，content，summary，category，channel，pic（图片），author（作者：session作用域中）
		//剩下的需要在service中加入：hits  hot   STATUS  deleted  created  UPDATEd
		blog.setHits(0);
		blog.setHot(false);
		blog.setDeleted(false);
		blog.setStatus(1);
		blog.setCreated(new Date());
		blog.setUpdated(blog.getCreated());
		int i=articleMapper.addBlog(blog);
		return i;
	}

	@Override
	public int removeBlogById(Integer id) {
		int i=articleMapper.removeBlogById(id);
		return i;
	}

	@Override
	public Article getArticleById(Integer id) {
		
		return articleMapper.getArticleById(id);
	}

	@Override
	public int updBlog(Article blog) {
	// 1：处理 摘要的内容 ：如果摘要不写 默认取 内容的前10个字 如果没有10个子就都取了
			if(!StringUtil.hasText(blog.getSummary())) {
				// 如果为null 或者空字符串包括空格那么 进来 处理
				String content = blog.getContent();
				String replaceAll = content.replaceAll("<.+?>", "");
				if(replaceAll.length()>10) {
					blog.setSummary(replaceAll.substring(0,10));
				}else {
					blog.setSummary(replaceAll);
				}
			}
		blog.setUpdated(new Date());
		int i=articleMapper.updBlog(blog);
		return i;
	}

	@Override
	public Article getBlogById(Integer id) {
		// 访问文章 hits 点击量加1
		articleMapper.increaseHit(id);
		Article article=articleMapper.getBlogById(id);
		return article;
	}

	@Override
	public List commentsByBlogId(Integer id) {
		// TODO Auto-generated method stub
		return articleMapper.commentsByBlogId(id);
	}

	@Override
	public List hitBlogs() {
		// TODO Auto-generated method stub
		return articleMapper.hitBolgs();
	}

	@Override
	public List blogList(Map map) {
		// TODO Auto-generated method stub
		return articleMapper.blogList(map);
	}

	@Override
	public int changeStatus(Integer id) {
		// TODO Auto-generated method stub
		return articleMapper.changeStatus(id);
	}
	
	
}
